package com.floreantpos.ui.model;

import java.awt.BorderLayout;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jdesktop.swingx.JXDatePicker;

import com.floreantpos.model.CouponAndDiscount;
import com.floreantpos.model.dao.CouponAndDiscountDAO;
import com.floreantpos.swing.FixedLengthDocument;
import com.floreantpos.swing.MessageDialog;
import com.floreantpos.ui.BeanEditor;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class CouponForm extends BeanEditor {

  private JPanel contentPane;
  private JTextField tfCouponName;
  private JCheckBox chkDisabled;
  private JCheckBox chkNeverExpire;
  private JXDatePicker dpExperation;

  public CouponForm() {
    this(new CouponAndDiscount());
  }

  public CouponForm(CouponAndDiscount coupon) {
    initComponents();

    this.setLayout(new BorderLayout());
    add(contentPane);

    tfCouponName.setDocument(new FixedLengthDocument(30));

    setBean(coupon);
  }

  @Override
  public boolean save() {
    try {
      if (!updateModel()) {
        return false;
      }

      CouponAndDiscount coupon = (CouponAndDiscount) getBean();
      CouponAndDiscountDAO dao = new CouponAndDiscountDAO();
      dao.saveOrUpdate(coupon);
    } catch (Exception e) {
      MessageDialog.showError(com.floreantpos.POSConstants.SAVE_ERROR, e);
      return false;
    }
    return true;
  }

  @Override
  public void dispose() {
  }

  @Override
  protected void updateView() {
    CouponAndDiscount coupon = (CouponAndDiscount) getBean();
    if (coupon == null) {
      return;
    }

    tfCouponName.setText(coupon.getName());
    dpExperation.setDate(coupon.getExpiryDate());
    chkDisabled.setSelected(coupon.isDisabled());
    chkNeverExpire.setSelected(coupon.isNeverExpire());
  }

  @Override
  protected boolean updateModel() {
    String name = tfCouponName.getText();
    double couponValue = 0;
    int couponType = 0;
    Date expiryDate = dpExperation.getDate();
    boolean disabled = chkDisabled.isSelected();
    boolean neverExpire = chkNeverExpire.isSelected();

    if (name == null || name.trim().equals("")) {
      MessageDialog.showError(com.floreantpos.POSConstants.NAME_REQUIRED);
      return false;
    }

    if (expiryDate == null && !neverExpire) {
      MessageDialog.showError(com.floreantpos.POSConstants.SET_DATE);
      return false;
    }

    CouponAndDiscount coupon = (CouponAndDiscount) getBean();
    coupon.setName(name);
    coupon.setValue(couponValue);

    if (neverExpire) {
      coupon.setExpiryDate(null);
    } else {
      coupon.setExpiryDate(expiryDate);
    }

    coupon.setType(couponType);
    coupon.setDisabled(disabled);
    coupon.setNeverExpire(neverExpire);

    return true;
  }

  @Override
  public String getDisplayText() {
    CouponAndDiscount coupon = (CouponAndDiscount) getBean();
    if (coupon.getId() == null) {
      return com.floreantpos.POSConstants.INCLUDE_DISCOUNT;
    }
    return com.floreantpos.POSConstants.CHANGE_DISCOUNT;
  }

  private void initComponents() {
    contentPane = new JPanel();
    contentPane.setLayout(new FormLayout("fill:d:noGrow,left:4dlu:noGrow,fill:d:grow,left:4dlu:noGrow,fill:100px:grow", "center:d:noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
    contentPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5), null));
    final JLabel label1 = new JLabel();
    label1.setText(com.floreantpos.POSConstants.FIRST_NAME);
    CellConstraints cc = new CellConstraints();
    contentPane.add(label1, cc.xy(1, 1));
    final JLabel label2 = new JLabel();
    label2.setText(com.floreantpos.POSConstants.EXPIRY_DATE);
    contentPane.add(label2, cc.xy(1, 5));
    tfCouponName = new JTextField();
    contentPane.add(tfCouponName, cc.xyw(3, 1, 3, CellConstraints.FILL, CellConstraints.DEFAULT));
    dpExperation = new JXDatePicker();
    contentPane.add(dpExperation, cc.xy(3, 5));
    chkDisabled = new JCheckBox();
    chkDisabled.setText(com.floreantpos.POSConstants.DISABLED);
    contentPane.add(chkDisabled, cc.xy(3, 9));
    chkNeverExpire = new JCheckBox();
    chkNeverExpire.setText(com.floreantpos.POSConstants.NEVER_EXPIRE);
    contentPane.add(chkNeverExpire, cc.xy(3, 11));
  }

  public JComponent getRootComponent() {
    return contentPane;
  }
}
